import { Injectable } from '@angular/core';
import { Headers, Http} from '@angular/http';
import 'rxjs/add/operator/toPromise';

@Injectable()
export class AjaxService {
  
  private headers = new Headers({'Content-Type': 'application/json'});
  
  constructor(
    private http: Http
  ) { }

  post(url: string, body?: any) {
    return this.http.post(url, JSON.stringify(body || {}), {headers: this.headers}).toPromise();
  }

  get(url: string, body?: any) {
    return this.http.get(url).toPromise();
  }

  jpost(url: string, body?: any) {
    return $.ajax({
      url: url,
      type: "POST",
      datType: "JSON",
      data: body == null ? {} : body
      // data: JSON.stringify(body || {}),
      // contentType: "application/json"
    });
  }

  jget(url: string, body?: any) {
    return $.ajax({
      url: url,
      type: "GET",
      datType: "JSON",
      data: body == null ? {} : body
    });
  }
}
